package com.example.myblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.example.myblog.entity.BlogMessage;
import com.example.myblog.entity.Like;
import com.example.myblog.exception.BaseException;
import com.example.myblog.mapper.BlogMessageMapper;
import com.example.myblog.mapper.LikeMapper;
import com.example.myblog.result.BaseCodeEnum;
import com.example.myblog.service.BlogMessageService;
import com.example.myblog.service.CommentService;
import com.example.myblog.service.LikeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.myblog.utils.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 叶兴
 * @since 2022-09-29
 */
@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Like> implements LikeService {

    LikeMapper likeMapper;


    BlogMessageService blogMessageService;

    CommentService commentService;

    @Autowired
    public void setCommentService(CommentService commentService) {
        this.commentService = commentService;
    }

    @Autowired
    public void setBlogMessageService(BlogMessageService blogMessageService) {
        this.blogMessageService = blogMessageService;
    }

    @Autowired
    public void setLikeMapper(LikeMapper likeMapper) {
        this.likeMapper = likeMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertLikeArticle(String blogId) throws BaseException {
        // 1. 获取当前点赞用户
        String username = UserThreadLocal.getUserDtoName();
        // 2. 判断当前文章是否存在
        if (!blogMessageService.ifBlogExists(blogId)) {
            throw new BaseException(BaseCodeEnum.NOT_FOUND);
        }
        // 3. 判断是否已经点赞过该文章
        if (hasLikeBlog(blogId)) {
            throw new BaseException(BaseCodeEnum.HAS_LIKED);
        }
        // 4. 往点赞表中插入一条记录
        Like like = new Like();
        like.setUserName(username);
        like.setBlogId(blogId);
        int insert = likeMapper.insert(like);
        if (insert != 1) {
            throw new BaseException(BaseCodeEnum.INSERT_FAILED);
        }
        // 5. 更新文章信息表
        blogMessageService.updateBlogMessageLikes(username,blogId);

        return 0;
    }



    @Override
    public boolean hasLikeBlog(String blogId) throws BaseException {
        String username = UserThreadLocal.getUserDtoName();
        QueryWrapper<Like> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",username).eq("blog_id",blogId);
        Like like = likeMapper.selectOne(wrapper);
        return like != null;
    }

    @Override
    public boolean hasLikeComment(Integer commentId) throws BaseException {
        String username = UserThreadLocal.getUserDtoName();
        QueryWrapper<Like> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",username).eq("comment_id",commentId);
        Like like = likeMapper.selectOne(wrapper);
        return like != null;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertLikeComment(String blogId,Integer commentId) throws BaseException {
        // 1. 获取当前点赞用户
        String username = UserThreadLocal.getUserDtoName();

        // 3. 判断是否已经点赞过该评论
        if (hasLikeComment(commentId)) {
            throw new BaseException(BaseCodeEnum.HAS_LIKED);
        }
        // 4. 往点赞表中插入一条记录
        Like like = new Like();
        like.setUserName(username);
        like.setCommentId(commentId);
        like.setBlogId(blogId);
        int insert = likeMapper.insert(like);
        if (insert != 1) {
            throw new BaseException(BaseCodeEnum.INSERT_FAILED);
        }
        // 5. 更新评论表
        commentService.updateCommentLikes(commentId,username);

        return 0;
    }
}
